package com.android.functionmanagement;

import java.io.File;
import java.util.HashMap;

import cn.jpush.android.api.InstrumentedActivity;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.globle.HttpObserver;
import com.android.functionmanagement.globle.LogGloble;
import com.android.functionmanagement.util.DeviceUtils;
import com.android.functionmanagement.util.UploadData;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class BaseActivity extends InstrumentedActivity implements HttpObserver {
	
	private static final String TAG = "BaseActivity";
	/**
	 * 通讯提示框
	 */
	private Dialog alertDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerReceiver(broadcastReceiver, new IntentFilter("com.android.action.GETUSERINFO"));
	}
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			updateDisplayData();
		}
	};
	protected void updateDisplayData() {
		
	}

	@Override
	public void resposeContent(Object ob) {
		LogGloble.d(TAG, ob+"");
	}

	@Override
	public void resposeResposeCode(int code) {
	}

	@Override
	public boolean httpRequestCallBackPre(Object resultObj) {
		if(resultObj!=null){
			if(resultObj instanceof HashMap){
				HashMap<String, Object> hashMap = (HashMap<String, Object>) resultObj;
				if(hashMap.containsKey(ConstantGloble.RESPHEAD)){
					if(hashMap.get(ConstantGloble.RESPHEAD) instanceof HashMap){
						HashMap<String, String> resp = (HashMap<String, String>) hashMap.get(ConstantGloble.RESPHEAD);
						if(resp!=null){
							String respcode = resp.get(ConstantGloble.RESPCODE);
							//非正常情况进行拦截
							if(!ConstantGloble.RESPCODE_SUCCESS.equals(respcode)){
								if (ConstantGloble.DEBUG) {
									LogGloble.d(TAG,
											resp.get(ConstantGloble.RESPMSG));
								}
								return true;
							}
						}
					}
				}
			}
		}
		return false;
	}

	@Override
	public boolean httpRequestCallBackAfter(Object resultObj) {
		return false;
	}

	/**
	 * 上传文件test..
	 */
	private void testUpLoadFile(String path) {
//		final HashMap<String, Object> copyFrom = new HashMap<String, Object>();
//		copyFrom.putAll(getreqHeadContent(ConstantGloble.W003));
//		HashMap<String, Object> data = new UploadData(this, 4, 0).getHashMap();
//		copyFrom.putAll(data);
//		ControlCenter.getInstanse().requestHttp(ConstantGloble.getBaseUri(this),
//				"post", null, copyFrom, new File(path), this, this);
	}
	
	@Override
	public void showAlert() {
		if (alertDialog == null) {
			alertDialog = new Dialog(this);
			alertDialog.setTitle("数据加载中");
		}
		if (!alertDialog.isShowing()) {
			alertDialog.show();
		}
	}

	@Override
	public void hideAlert() {
		if (alertDialog != null && alertDialog.isShowing()) {
			alertDialog.dismiss();
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(alertDialog!=null){
			alertDialog.dismiss();
		}
		unregisterReceiver(broadcastReceiver);
	}
	
	/**
     * Activtiy 跳转公共方法
     *
     * @param activity  目标Activity
     * @param data      传输数据
     * @param enterAnim 进入动画
     * @param exitAnim  退出动画
     */
    public void goActivity(Class<?> activity, HashMap<String, Object> data, int enterAnim, int exitAnim) {
    	Bundle bundle = new Bundle();
    	bundle.putSerializable(ConstantGloble.SENDDATA, data);
        Intent intent = new Intent(this, activity);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(enterAnim, exitAnim);
    }
    /**
	 * getSendData:跳转获取数据. <br/>
	 * 
	 * @return
	 * @author fmh
	 * @since 1.0
	 */
	protected Object getSendData() {
		if (getIntent().getExtras() != null) {
			if (getIntent().getExtras()
					.getSerializable(ConstantGloble.SENDDATA) != null) {
				return getIntent().getExtras().getSerializable(
						ConstantGloble.SENDDATA);
			}
		}
		return null;
	}
	
	/**
	 * 获取请求头报文
	 * @param functionId
	 * @return
	 */
	public HashMap<String, String> getreqHeadContent(String functionId){
		return DeviceUtils.getreqHeadContent(functionId, this);
	}
	
}
